﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using WarehouseManagement.BusinessLogic.BC;

namespace WarehouseManagement.Views.Merchandise
{
    public partial class NewEditMerchandiseCategory : DevExpress.XtraEditors.XtraForm
    {
        private bool _isEditMode;
        private MerchandiseBC _merchandiseBC;

        public delegate void OperationCompleted(bool success, bool isEditMode);
        public OperationCompleted OnOperationCompleted;

        public NewEditMerchandiseCategory(bool isEditMode = false)
        {
            InitializeComponent();
            _isEditMode = isEditMode;
            _merchandiseBC = new MerchandiseBC();
            if (isEditMode)
            {
                this.Text = "Sửa nhóm hàng";
                
            }
            else
            {
                this.Text = "Thêm nhóm hàng";
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var errList = ValidateCategory(_isEditMode);
            if (errList.Count() > 0)
            {
                XtraMessageBox.Show(string.Join("\n", errList), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                var dto = new BusinessLogic.DTO.MerchandiseCategoryDTO()
                    {
                        Id = this.Id,
                        MerchandiseCategoryName = CategoryName,
                        Notes = Notes
                    };
                if (!_isEditMode)
                {
                    _merchandiseBC.AddCategory(dto);
                }
                else
                {
                    _merchandiseBC.UpdateCategory(dto);
                }
                FireOperationCompleted(true, _isEditMode);
                this.Close();
            }
            catch (Exception)
            {
                FireOperationCompleted(false, _isEditMode);
            }
        }
        private void FireOperationCompleted(bool success, bool isEditMode)
        {
            if (OnOperationCompleted != null)
                OnOperationCompleted(success, isEditMode);
        }
        public string CategoryName
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }

        public string Notes
        {
            get { return txtNotes.Text; }
            set { txtNotes.Text = value; }
        }

        public int Id { get; set; }

        private IEnumerable<string> ValidateCategory(bool editMode)
        {
            if (string.IsNullOrEmpty(CategoryName))
                yield return "Tên nhóm không được bỏ trống";
            if (!editMode)
            {
                if (_merchandiseBC.ContainCategory(CategoryName.Trim()))
                {
                    yield return "Nhóm này đã tồn tại";
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}